diff options
| author | Factiven <[email protected]> | 2023-12-24 13:03:54 +0700 |
|---|---|---|
| committer | Factiven <[email protected]> | 2023-12-24 13:03:54 +0700 |
| commit | 50a0f0240d7fef133eb5acc1bea2b1168b08e9db (patch) | |
| tree | 307e09e505580415a58d64b5fc3580e9235869f1 /pages/api/auth/[...nextauth].ts | |
| parent | Update README.md (#104) (diff) | |
| download | moopa-50a0f0240d7fef133eb5acc1bea2b1168b08e9db.tar.xz moopa-50a0f0240d7fef133eb5acc1bea2b1168b08e9db.zip | |
migrate to typescript
Diffstat (limited to 'pages/api/auth/[...nextauth].ts')
| -rw-r--r-- | pages/api/auth/[...nextauth].ts | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts new file mode 100644 index 0000000..70b2e3d --- /dev/null +++ b/pages/api/auth/[...nextauth].ts @@ -0,0 +1,125 @@ +import NextAuth, { NextAuthOptions } from "next-auth"; + +export const authOptions: NextAuthOptions = { + secret: process.env.NEXTAUTH_SECRET, + providers: [ + { + id: "AniListProvider", + name: "AniList", + type: "oauth", + token: "https://anilist.co/api/v2/oauth/token", + authorization: { + url: "https://anilist.co/api/v2/oauth/authorize", + params: { scope: "", response_type: "code" }, + }, + userinfo: { + url: process.env.GRAPHQL_ENDPOINT, + async request(context) { + // console.log(context.tokens.access_token); + const { data } = await fetch("https://graphql.anilist.co", { + method: "POST", + headers: { + "Content-Type": "application/json", + // ...(context.tokens.access_token && { + Authorization: `Bearer ${context.tokens.access_token}`, + // }), + }, + body: JSON.stringify({ + query: ` + query { + Viewer { + id + name + avatar { + large + medium + } + bannerImage + mediaListOptions { + animeList { + customLists + } + } + } + } + `, + }), + }).then((res) => res.json()); + + const userLists = data.Viewer?.mediaListOptions.animeList.customLists; + + let custLists = userLists || []; + + if (!userLists?.includes("Watched using Moopa")) { + custLists.push("Watched using Moopa"); + const fetchGraphQL = async ( + query: string, + variables: { lists: any } + ) => { + const response = await fetch("https://graphql.anilist.co/", { + method: "POST", + headers: { + "Content-Type": "application/json", + ...(context.tokens.access_token && { + Authorization: `Bearer ${context.tokens.access_token}`, + }), + }, + body: JSON.stringify({ query, variables }), + }); + return response.json(); + }; + + const customLists = async (lists: any) => { + const setList = ` + mutation($lists: [String]){ + UpdateUser(animeListOptions: { customLists: $lists }){ + id + } + } + `; + const data = await fetchGraphQL(setList, { lists }); + return data; + }; + + await customLists(custLists); + } + + return { + token: context.tokens.access_token, + name: data.Viewer.name, + sub: data.Viewer.id, + image: data.Viewer.avatar, + list: data.Viewer?.mediaListOptions.animeList.customLists, + }; + }, + }, + clientId: process.env.CLIENT_ID, + clientSecret: process.env.CLIENT_SECRET, + profile(profile) { + return { + token: profile.token, + id: profile.sub, + name: profile?.name, + image: profile.image, + list: profile?.list, + version: "1.0.1", + }; + }, + }, + ], + session: { + //Sets the session to use JSON Web Token + strategy: "jwt", + }, + callbacks: { + async jwt({ token, user }) { + return { ...token, ...user }; + }, + async session({ session, token, user }) { + session.user = token; + return session; + }, + }, +}; + +export default NextAuth(authOptions); |